/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.AdCategoryEntity;
import cn.eclass.entity.AdvertiseEntity;
import cn.eclass.entity.Entity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class AdvertiseDao extends CommonDao {

    private static final String TABLE_NAME = "advertise";

    public AdvertiseDao() {
        super(AdvertiseDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        AdvertiseEntity advertiseEntity = new AdvertiseEntity();
        try {
            advertiseEntity.setId(resultSet.getInt("ID"));
            advertiseEntity.setLink(resultSet.getString("LINK"));
            advertiseEntity.setOrder(resultSet.getInt("ORDER"));
            advertiseEntity.setPicture(resultSet.getString("PICTURE"));
            advertiseEntity.setDescription(resultSet.getString("DESCRIPTION"));
            advertiseEntity.setAdcatId(resultSet.getInt("ADCATID"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return advertiseEntity;
    }

    public int add(AdvertiseEntity advertise) {
        String sql = "insert into " + TABLE_NAME
                + "(`LINK`,`ORDER`,`PICTURE`,`DESCRIPTION`,`ADCATID`) "
                + "values ("
                + "'" + advertise.getLink() + "',"
                + advertise.getOrder() + ","
                + "'" + advertise.getPicture() + "',"
                + "'" + advertise.getDescription() + "',"
                + advertise.getAdcatId()
                + ")";
        return super.insert(sql);
    }

    public boolean update(AdvertiseEntity advertise) {
        String sql = "update " + TABLE_NAME + " set "
                + "`LINK` = '" + advertise.getLink() + "',"
                + "`ORDER` = " + advertise.getOrder() + ","
                + "`PICTURE` = '" + advertise.getPicture() + "',"
                + "`DESCRIPTION` = '" + advertise.getDescription() + "',"
                + "`ADCATID` = " + advertise.getAdcatId()
                + " where id = " + advertise.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public AdvertiseEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<AdvertiseEntity> advertiseEntitys = (List<AdvertiseEntity>) super.select(sql);
        return advertiseEntitys.get(0);
    }

    public List<AdvertiseEntity> list() {
        String whereSql = "1";
        return list(whereSql);
    }

    public List<AdvertiseEntity> list(String whereSql) {
        String sql = "select * from " + TABLE_NAME + " where " + whereSql;
        return (List<AdvertiseEntity>) super.select(sql);
    }

    public List<AdvertiseEntity> getByCat(int catId) {
        String sql = "select * from " + TABLE_NAME + " where adcatid = " + catId;
        return (List<AdvertiseEntity>) super.select(sql);
    }

    public List<AdvertiseEntity> list(AdCategoryEntity adCategoryEntity) {
        String whereSql = " adcatId = " + adCategoryEntity.getId();
        return list(whereSql);
    }
}
